1 package cz
.brmlab
.brmtron
.server
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.DataOutputStream
;
5 import java
.io
.InputStreamReader
;
6 import java
.net
.Socket
;
8 public class BrmTronServerThread
extends Thread
{
12 private Player player
;
15 public BrmTronServerThread(Socket sock
, int id
){
16 super("BrmTronServerThread");
19 this.player
= new Player(id
);
27 DataOutputStream out
= new DataOutputStream(sock
.getOutputStream());
28 BufferedReader in
= new BufferedReader(new InputStreamReader(sock
.getInputStream()));
30 player
.setActive(true);
32 while((s
=in
.readLine()) != null) {
34 if(s
.equalsIgnoreCase("die")){
35 player
.setActive(false);
37 if(s
.equals("quit")) {
42 Player np
= ProtocolHelper
.parsePlayer(s
);
45 PathElement elem
= new PathElement(player
.getPrevCoords(), player
.getCoords());
48 double[] tmp
= BrmTronServer
.checkForCollision(elem
);
50 //add a wall; if player has crashed, change his end position to a crash location
52 BrmTronServer
.addWall(elem
);
54 elem
.setEnd(tmp
[0], tmp
[1]);
55 BrmTronServer
.addWall(elem
);
56 player
.setActive(false);
57 player
.setCoords(tmp
[0], tmp
[1]);
60 //save player state and send him an update
61 BrmTronServer
.updatePlayer(id
, player
);
62 out
.writeBytes(ProtocolHelper
.getUpdate(BrmTronServer
.getPlayers()));
68 } catch(Exception e
) {
69 System
.err
.println(e
);